#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e5 + 5;

int h[N], e[N], ne[N], w[N], idx;
void add(int a, int b, int c)
{
    e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx++;
}

int dist[N], n, m;
bool st[N];

void spfa()
{
    queue<int> q;
    q.push(1);
    dist[1] = 0;
    st[1] = true;
    while(q.size())
    {
        int cur = q.front(); q.pop();
        st[cur] = false;
        for(int i = h[cur]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > dist[cur] + w[i])
            {
                dist[j] = dist[cur] + w[i];
                if(!st[j])
                {
                    st[j] = true;
                    q.push(j);
                }
            }
        }
    }
    if(dist[n] == 0x3f3f3f3f) cout << "impossible";
    else cout << dist[n];
}

int main()
{
    memset(h, -1, sizeof h);
    memset(dist, 0x3f, sizeof dist);
    cin >> n >> m;
    while(m--)
    {
        int a, b, c; cin >> a >> b >> c;
        add(a, b, c);
    }
    spfa();
    return 0;
}